﻿using org.jsoup;
using org.jsoup.nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CodeBeautifier
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            initDbType();
        }
        public string getCodeType()
        {
            string code_type = null;
            if (radioButton1.IsChecked == true)
            {
                code_type = "json";
            }
            else if (radioButton2.IsChecked == true)
            {
                code_type = "xml";
            }
            else if (radioButton3.IsChecked == true)
            {
                code_type = "html";
            }
            else if (radioButton4.IsChecked == true)
            {
                code_type = "sql";
            }

            return code_type;
        }

        //格式化
        private void Format()
        {
            checkNull();
            string code_type = getCodeType();
            string str_source = inputTextBox.Text;           
            string str_result = null;

            if (code_type == "html")
            {
                Document doc = Jsoup.parse(str_source);
                str_result = doc.outerHtml();
            }
            else if (code_type == "json")
            {
                str_result = CodeFormatAndCompress.FormatForJson(str_source);
            }
            else if (code_type == "xml")
            {
                str_result = CodeFormatAndCompress.FormatForXml(str_source);
            }
            else if (code_type == "sql")
            {
                string dbType = this.dbSelectComboBox.Text; 
                str_result = CodeFormatAndCompress.FormatForSql(str_source, dbType.ToLower());
            }
            outputTextBox.Text = str_result;
        }

        //压缩
        private void Compress()
        {
            checkNull();
            string code_type = getCodeType();
            string str_source = inputTextBox.Text;

            if (code_type == "html")
            { 
                String str_result = CodeFormatAndCompress.CompressForXml(str_source);
                outputTextBox.Text = str_result;
            }
            else if (code_type == "json")
            {
                String str_result = CodeFormatAndCompress.CompressForJson(str_source);
                outputTextBox.Text = str_result;
            }
            else if (code_type == "xml")
            {
                String str_result = CodeFormatAndCompress.CompressForXml(str_source);
                outputTextBox.Text = str_result;
            }
            else if (code_type == "sql")
            {
                string dbType = this.dbSelectComboBox.Text;
                String str_result = CodeFormatAndCompress.CompressForSql(str_source, dbType);
                outputTextBox.Text = str_result;
            }
        }
        private void checkNull() {
            string code_type = getCodeType();
            if (code_type == null || code_type == "")
            {
                MessageBox.Show("请先选择需要美化的代码语言！");
                return;
            }

            string str_source = inputTextBox.Text;
            if (str_source == null || str_source == "")
            {
                MessageBox.Show("请输入需要美化的代码！");
                return;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Format();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Compress();
            //myGrid.Visibility = Visibility.Collapsed;
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            inputTextBox.Clear();
            outputTextBox.Clear();
        }

        private void radioButton4_Checked(object sender, RoutedEventArgs e)
        {
            //Visible  Visibility
            this.dbSelectComboBox.Visibility= Visibility.Visible;
             
        }

        private void initDbType() {
            List<string> dbTypeList = new List<string>();
            dbTypeList.Add("Oracle");
            dbTypeList.Add("MySql");
            dbTypeList.Add("PostgreSql");
            dbTypeList.Add("SqlServer");
            dbTypeList.Add("Hive");
            dbTypeList.Add("Odps");
            this.dbSelectComboBox.ItemsSource= dbTypeList;
            this.dbSelectComboBox.SelectedIndex = 1;
        }
    }
}
